<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="问题标题" prop="questionTitle">
        <el-input
          v-model="queryParams.questionTitle"
          placeholder="请输入问题标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户端" prop="clientSide">
        <el-select v-model="queryParams.clientSide" placeholder="请选择">
          <el-option
            label="全部类型"
            value="99">
          </el-option>
          <el-option
            label="快递员"
            value="0">
          </el-option>
          <el-option
            label="会员端"
            value="1">
          </el-option>
          <el-option
            label="智能柜终端"
            value="2">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="问题分类" prop="questionCategorizationName">
        <el-select v-model="queryParams.questionCategorizationName" placeholder="请选择问题分类">
          <el-option
            label="全部类型"
            value="99">
          </el-option>
          <el-option
            v-for="c in categorizationList"
            :key="c.id"
            :label="c.chineseNomenclature"
            :value="c.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="语言" prop="language">
        <el-select v-model="queryParams.language" placeholder="请选择语言">
          <el-option
            label="全部类型"
            value="99">
          </el-option>
          <el-option
            label="中文"
            value="0">
          </el-option>
          <el-option
            label="英文"
            value="1">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="questionStatus">
        <el-select v-model="queryParams.questionStatus" placeholder="请选择状态">
          <el-option
            label="全部类型"
            value="99">
          </el-option>
          <el-option
            label="启用"
            value="0">
          </el-option>
          <el-option
            label="禁用"
            value="1">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" prop="time">
        <el-date-picker
          v-model="queryParams.time"
          type="daterange"
          value-format="yyyy-MM-dd"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['question:question:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['question:question:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['question:question:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['question:question:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="questionList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id">
        <template slot-scope="scope">
          <el-button type="text" @click="handleUpdate(scope.row)" v-hasPermi="['question:question:edit']">{{scope.row.id}}</el-button>
        </template>
      </el-table-column>
      <el-table-column label="问题标题" align="center" prop="questionTitle" />
      <el-table-column label="客户端" align="center" prop="clientSide">
        <template slot-scope="scope">
          {{scope.row.clientSide==0?'快递员':scope.row.clientSide==1?'会员端':'智能柜终端'}}
        </template>
      </el-table-column>
      <el-table-column label="问题分类" align="center" prop="questionCategorizationName" />
      <el-table-column label="语言" align="center" prop="language">
        <template slot-scope="scope">
          {{scope.row.language==0?'中文':'英文'}}
        </template>
      </el-table-column>
      <el-table-column label="排序号" align="center" prop="sequenceNumber" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="questionStatus">
        <template slot-scope="scope">
          {{scope.row.questionStatus==0?'正常':'已禁用'}}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            v-if="scope.row.questionStatus==0"
            @click="updateEQuestionStatus(scope.row)"
          >禁用</el-button>
          <el-button
            size="mini"
            type="text"
            v-if="scope.row.questionStatus==1"
            @click="updateEQuestionStatus(scope.row)"
          >启用</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['question:question:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加问题列表对话框 -->
    <el-dialog :title="title" :visible.sync="openInsert" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="客户端" prop="clientSide">
          <el-radio-group v-model="form.clientSide">
            <el-radio :label="0">快递员</el-radio>
            <el-radio :label="1">会员端</el-radio>
            <el-radio :label="2">智能柜终端</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="语言" prop="language">
          <el-radio-group v-model="form.language">
            <el-radio :label="0">中文</el-radio>
            <el-radio :label="1">英文</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="问题分类" prop="cid">
          <el-select v-model="form.cid" placeholder="请选择问题分类">
            <el-option
              v-for="c in categorizationList"
              :key="c.id"
              :label="c.chineseNomenclature"
              :value="c.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="问题标题" prop="questionTitle">
          <el-input v-model="form.questionTitle" placeholder="请输入问题标题" />
        </el-form-item>
        <el-form-item label="标题内容" prop="questionContent">
          <el-input type="textarea" v-model="form.questionContent" placeholder="请输入标题内容"></el-input>
        </el-form-item>
        <el-form-item label="排序号" prop="sequenceNumber">
          <el-input v-model="form.sequenceNumber" placeholder="请输入排序号" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 修改问题列表对话框 -->
    <el-dialog :title="title" :visible.sync="openUpdate" width="500px" append-to-body>
      <el-form ref="pojo" :model="pojo" :rules="rules" label-width="80px">
        <el-form-item label="客户端" prop="clientSide">
          <el-radio-group v-model="pojo.clientSide">
            <el-radio :label="0">快递员</el-radio>
            <el-radio :label="1">会员端</el-radio>
            <el-radio :label="2">智能柜终端</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="语言" prop="language">
          <el-radio-group v-model="pojo.language">
            <el-radio :label="0">中文</el-radio>
            <el-radio :label="1">英文</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="问题分类" prop="cid">
          <el-select v-model="pojo.cid" placeholder="请选择问题分类">
            <el-option
              v-for="c in categorizationList"
              :key="c.id"
              :label="c.chineseNomenclature"
              :value="c.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="问题标题" prop="questionTitle">
          <el-input v-model="pojo.questionTitle" placeholder="请输入问题标题" />
        </el-form-item>
        <el-form-item label="标题内容" prop="questionContent">
          <el-input type="textarea" v-model="pojo.questionContent" placeholder="请输入标题内容"></el-input>
        </el-form-item>
        <el-form-item label="排序号" prop="sequenceNumber">
          <el-input v-model="pojo.sequenceNumber" placeholder="请输入排序号" />
        </el-form-item>
        <el-form-item label="创建时间" prop="createTime">
          {{pojo.createTime}}
        </el-form-item>
        <el-form-item label="最后修改时间" prop="lastModifiedTime">
          {{pojo.lastModifiedTime==null?'该问题还未被修改':pojo.lastModifiedTime}}
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitPojo">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listQuestion, getQuestion, delQuestion, addQuestion, updateQuestion, updateEQuestionStatus } from "@/api/question/question";
import { selectChineseNomenclatureList, updateCategorizationReferenceStatus } from "@/api/question/categorization";

export default {
  name: "Question",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 问题列表表格数据
      questionList: [],
      // 弹出层标题
      title: "",
      // 添加问题界面是否显示弹出层
      openInsert: false,
      // 编辑问题界面是否显示弹出层
      openUpdate: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        questionTitle: null,
        clientSide: '99',
        cid: null,
        questionCategorizationName:'99',
        language: '99',
        sequenceNumber: null,
        createTime: null,
        questionStatus: '99',
        time:null,
      },
      // 表单参数
      form: {},
      // 编辑问题界面的表单参数
      pojo: {},
      // 表单校验
      rules: {
        clientSide:[
          {required:true,message:"请选择客户端",trigger:'blur'}
        ],
        language:[
          {required:true,message:"请选择语言",trigger:'blur'}
        ],
        cid:[
          {required:true,message:"请选择问题分类",trigger:'blur'}
        ],
        questionTitle:[
          {required:true,message:"请填写问题标题",trigger:'blur'}
        ],
        questionContent:[
          {required:true,message:"请填写标题内容",trigger:'blur'}
        ],
        // sequenceNumber:[
        //   {required:true,message:"请填写排序号",trigger:'blur'}
        // ]
      },
      //问题分类列表
      categorizationList:null,
    };
  },
  created() {
    this.getList();
    this.selectChineseNomenclatureList();
  },
  activated() {
    this.getList();
    this.selectChineseNomenclatureList();
  },
  methods: {
    /** 查询问题列表列表 */
    getList() {
      this.loading = true;
      listQuestion(this.queryParams).then(response => {
        this.questionList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.openInsert = false;
      this.openUpdate = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        questionTitle: null,
        clientSide: null,
        cid: null,
        language: null,
        sequenceNumber: null,
        createTime: null,
        questionStatus: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      if(this.queryParams.time!=null){
        const time=this.queryParams.time;
        this.queryParams.startCreateTime=time[0];
        this.queryParams.endCreateTime=time[1];
      }else{
        this.queryParams.startCreateTime=null;
        this.queryParams.endCreateTime=null;
      }
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.openInsert = true;
      this.title = "添加问题列表";
    },
    //查询所有问题分类列表的中文名称
    selectChineseNomenclatureList(){
      selectChineseNomenclatureList().then(response=>{
        this.categorizationList=response.data;
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getQuestion(id).then(response => {
        this.pojo = response.data;
        this.openUpdate = true;
        this.title = "修改问题列表";
      });
    },
    /** 添加提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
            addQuestion(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.openInsert = false;
              this.getList();
            });
        }
      });
    },
    /** 修改提交按钮 */
    submitPojo(){
      this.$refs["pojo"].validate(valid => {
        if (valid) {
          const id=this.pojo.id;
          updateCategorizationReferenceStatus(id).then(response=>{
            updateQuestion(this.pojo).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.openUpdate = false;
              this.getList();
            });
          });

        }
      })
    },
    updateEQuestionStatus(row){
      if(row.questionStatus=='0'){
        this.$confirm('您确定要禁用该问题吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          updateEQuestionStatus(row).then(response=>{
            this.getList();
            this.$message({
              type: 'success',
              message: '禁用成功!'
            });
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消禁用'
          });
        });
      }else{
        this.$confirm('您确定要启用该问题吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          updateEQuestionStatus(row).then(response=>{
            this.getList();
            this.$message({
              type: 'success',
              message: '启用成功!'
            });
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消启用'
          });
        });
      }
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除问题列表编号为"' + ids + '"的数据项？').then(function() {
        return delQuestion(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('question/question/export', {
        ...this.queryParams
      }, `question_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
